package com.xxx.method;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/26 14:34
 * @Copyright 博为峰
 */
public class MethodRef {
    /*
       方法引用: 操作符(::),通过方法的名字来使用方法
       1.对象::实例方法名
       2.类::静态方法名
       3.类::实例方法名
       4.构造方法引用

       要求: 实现接口的抽象方法的参数列表和返回值类型，必须与方法引用的方法的参数列表和返回值类型保持一致

     */
    public static void main(String[] args) {
//        m1();
//        m2();
//        m3();
//        Student student = new Student();

        Supplier<Student> supplier = new Supplier<Student>() {
            @Override
            public Student get() {
                return new Student();
            }
        };

        Supplier<Student> supplier2 = () -> new Student();

        Supplier<Student> supplier3 = Student::new;


    }

    private static void m3() {
        Function<Double, Long> function = new Function<Double, Long>() {
            @Override
            public Long apply(Double aDouble) {
                return Math.round(aDouble);
            }
        };
//        System.out.println(function.apply(3.56));

        Function<Double, Long> function2 = d -> Math.round(d);

        Function<Double, Long> function3 = Math::round;
    }

    private static void m2() {
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        };
//        System.out.println(comparator.compare(6, 5));

        Comparator<Integer> comparator2 = (Integer o1, Integer o2) -> o1 - o2;
        Comparator<Integer> comparator3 = (Integer o1, Integer o2) -> Integer.compare(o1, o2);
        Comparator<Integer> comparator4 = Integer::compare;
        System.out.println(comparator4.compare(6, 5));
    }

    private static void m1() {
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
//        consumer.accept("abc");
        Consumer<String> consumer2 = s -> System.out.println(s);
//        consumer2.accept("def");
        PrintStream out = System.out;
//        Consumer<String> consumer3 = System.out::println;
        Consumer<String> consumer3 = out::println;
        consumer2.accept("123");
    }
}
